#include "widget_network.h"
#include "players.h"
#include "gui.h"
#include <QTcpSocket>
#include <sstream>



network_widget::network_widget( player* pl, QPushButton* nb, QWidget* parent, Qt::WindowFlags flags): QWidget( parent, flags )
{
	next_b = nb;
	network_client::get_instance()->client = new Client();
	current_player = pl;
	layout = new QVBoxLayout( this );


	QLabel* title_l = new QLabel( this );
	title_l->setTextFormat( Qt::RichText );
	std::ostringstream sout;
	sout << "<font size=6>" << current_player->get_name() << " :</font>";
	title_l->setText( sout.str().c_str() );
	layout->addWidget( title_l );


	QWidget* connect_w = new QWidget( this );
	QHBoxLayout* connect_w_l = new QHBoxLayout( connect_w );

	QLabel* address_l = new QLabel( "IP Address", connect_w );
	connect_w_l->addWidget( address_l );

	address_i = new QLineEdit( connect_w );
	address_i->setFixedWidth( 100 );
	address_i->setText( "127.0.0.1" );
	connect_w_l->addWidget( address_i );

	QLabel* port_l = new QLabel( "Port", connect_w );
	connect_w_l->addWidget(port_l);

	port_i = new QLineEdit( connect_w );
	port_i->setFixedWidth( 50 );
	port_i->setText( "1234" );
	connect_w_l->addWidget( port_i );

	QPushButton* connect_b = new QPushButton( "Connect", connect_w );
	connect_b->setFixedWidth( 150 );
	connect_w_l->addWidget( connect_b );
	connect( connect_b, SIGNAL( clicked() ), this, SLOT( network_connect() ) );

	connect_w->setLayout(connect_w_l);
	layout->addWidget(connect_w);


	status_l = new QLabel();
	status_l->setTextFormat( Qt::RichText );
	layout->addWidget( status_l );


	this->setLayout( layout );
};



network_widget::~network_widget()
{
	
};



void network_widget::set_status( std::string s )//QAbstractSocket::SocketState state)
{
	std::ostringstream sout;
	sout << "<b><font size=4>" << s << "</font></b>";
	/*switch(state)
	{
	case QAbstractSocket::UnconnectedState:
		sout << "<b><font size=4>The socket is not connected.</font></b>";
		break;
	case QAbstractSocket::HostLookupState:
		sout << "<b><font size=4>The socket is performing a host name lookup.</font></b>";
		break;
	case QAbstractSocket::ConnectingState:
		sout << "<b><font size=4>The socket has started establishing a connection.</font></b>";
		break;
	case QAbstractSocket::ConnectedState:
		sout << "<b><font size=4>A connection is established.</font></b>";
		break;
	case QAbstractSocket::BoundState:
		sout << "<b><font size=4>The socket is bound to an address and port (for servers).</font></b>";
		break;
	case QAbstractSocket::ListeningState:
		sout << "<b><font size=4>For internal use only.</font></b>";
		break;
	case QAbstractSocket::ClosingState:
		sout << "<b><font size=4>	The socket is about to close (data may still be waiting to be written).</font></b>";
		break;
	}*/
	status_l->setText( sout.str().c_str() );
};



void network_widget::network_connect()
{
	set_status( "Connecting" );
	network_client::get_instance()->client->connectToHost( address_i->text(), atoi(port_i->text().toStdString().c_str() ) );
	connect( network_client::get_instance()->client, SIGNAL( connected() ), this, SLOT( slot_connected() ) );
};



void network_widget::slot_connected()
{
	set_status( "Connected!" );
};